package cn.kgc.javaweb.servlet;

import cn.hutool.json.JSONUtil;
import cn.kgc.javaweb.dto.UserPageDTO;
import cn.kgc.javaweb.entity.Repair;
import cn.kgc.javaweb.service.RepairService;
import cn.kgc.javaweb.service.impl.RepairServiceImpl;
import cn.kgc.javaweb.util.JsonResultWriter;
import cn.kgc.javaweb.util.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 涛涛
 * @version 1.0
 */
@WebServlet("/repair")
public class RepairServlet extends BaseServlet{

    private RepairService repairService =new RepairServiceImpl();

    //分页查询
    protected void selectPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        String searchParams = req.getParameter("searchParams");
        UserPageDTO userPageDTO = UserPageDTO.builder().limit(limit).page(page).searchParams(searchParams).build();
        ResponseResult responseResult = repairService.selectPage(userPageDTO);
        JsonResultWriter.writerJson(resp,responseResult);

    }
    //公告-开关状态
    protected void state(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        boolean checked = Boolean.parseBoolean(req.getParameter("checked"));
        Integer id = Integer.valueOf(req.getParameter("id"));
        System.out.println(id);

        System.out.println(checked);
        repairService.state(checked,id);

    }

    // 修改用户信息
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // json 字符串
        String repair1 = req.getParameter("repair");
        Repair repair = JSONUtil.toBean(repair1, Repair.class);
        repair.setId(Integer.valueOf(req.getParameter("id")));
        System.out.println(repair);

        ResponseResult responseResult = repairService.update(repair);
        JsonResultWriter.writerJson(resp, responseResult);

    }

    // 添加用户信息
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String repair1 = req.getParameter("repair");
        Repair repair = JSONUtil.toBean(repair1, Repair.class);

        ResponseResult responseResult = repairService.add(repair);
        JsonResultWriter.writerJson(resp, responseResult);
    }

    // 删除用户信息
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String ids = req.getParameter("ids");

        ResponseResult responseResult =  repairService.delete(ids);
        JsonResultWriter.writerJson(resp, responseResult);


    }














}
